﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MicrobloggingInterface;

namespace SinaMicrobloggingClientUI
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            MainForm.CheckForIllegalCrossThreadCalls = false;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            JobStatus timerState = new JobStatus();

            // 创建代理对象TimerCallback，该代理将被定时调用.
            System.Threading.TimerCallback timerDelegate = new System.Threading.TimerCallback(CheckStatus);

            // 创建一个时间间隔为1s的定时器.
            System.Threading.Timer timer = new System.Threading.Timer(timerDelegate, timerState, 1000, 5000);
            timerState.timer = timer;

            // 主线程停下来等待Timer对象的终止.
            //while (timerState.timer != null)
            //    System.Threading.Thread.Sleep(0);

            //Console.WriteLine("Timer example done.");
            textBox1.Text += "\r\nTimer example done.";
        }

        private void CheckStatus(Object state)
        {
            JobStatus timerState = (JobStatus)state;
            timerState.counter++;

            //Console.WriteLine("{0} Checking Status {1}.", DateTime.Now.TimeOfDay, timerState.counter);
            string text = string.Format("{0} , {1} : ", DateTime.Now.TimeOfDay, timerState.counter);
            textBox1.Text += "\r\n" + text + testSend();

            if (timerState.counter == 5)
            {
                // 使用Change方法改变了时间间隔.
                (timerState.timer).Change(5000, 8000);

                //Console.WriteLine("changed");
                textBox1.Text += "\r\n" + "changed";
            }

            if (timerState.counter == 10)
            {
                //Console.WriteLine("disposing of timer");
                textBox1.Text += "\r\n" + "disposing of timer";

                timerState.timer.Dispose();
                timerState.timer = null;
            }
        }







        private string testSend()
        {
            string errorMessage = string.Empty;
            string userName = string.Empty;
            string password = string.Empty;
            string content = string.Empty;

            userName = "dennis_zsc";
            password = "dennis&quiet";
            content = Guid.NewGuid().ToString();

            // IssueMicroblogging(userName, password, content, out errorMessage);
            if (IssueMicroblogging(userName, password, content, out errorMessage))
            {
                return "Success";
            }
            else
            {
                return "Failed: " + errorMessage;
            }
        }

        private string testSend(int jobIndex, int counter,
                                string userName, string password,
                                string content, out string errorMessage)
        {
            errorMessage = string.Empty;
            string textContent = string.Format("Job Index: {0}, Count: {1}, Content: {2}", jobIndex, counter, content);

            // IssueMicroblogging(userName, password, content, out errorMessage);
            if (IssueMicroblogging(userName, password, textContent, out errorMessage))
            {
                return "Success: " + textContent;
            }
            else
            {
                return "Failed: " + errorMessage;
            }
        }

        private bool IssueMicroblogging(string userName, string password,
                                        string content, out string errorMessage)
        {
            //Dictionary<string, string> assambliesInfo = SearchAssamblies();

            IMicroblogging mb = null;
            MicrobloggingFactory factory = new MicrobloggingFactory();
            mb = factory.GetMicrobloggingType("HexunMicroblogging", "HexunMicroblogging.HexunMBImp");
            errorMessage = string.Empty;
            if (mb == null)
            {
                errorMessage = "factory.GetMicrobloggingType failed.";
                return false;
            }

            if (!mb.Issue(userName, password, content))
            {
                errorMessage = mb.GetLastError();
                return false;
            }

            return true;
        }

        private List<JobStatus> jobStatusList = new List<JobStatus>();

        private void button2_Click(object sender, EventArgs e)
        {
            if (jobStatusList.Count.Equals(2))
            {
                return;
            }

            JobStatus jobStatus = new JobStatus();

            // 创建代理对象TimerCallback，该代理将被定时调用.
            System.Threading.TimerCallback timerDelegate = new System.Threading.TimerCallback(timerElesped);

            // 创建一个时间间隔为1s的定时器.
            System.Threading.Timer timer = null;
            if (jobStatusList.Count.Equals(0))
            {
                timer = new System.Threading.Timer(timerDelegate, jobStatus, 1000, 5000);
            }
            else
            {
                timer = new System.Threading.Timer(timerDelegate, jobStatus, 2000, 3000);
            }
            
            jobStatus.timer = timer;
            jobStatus.userName = "dennis_zsc";
            jobStatus.password = "dennis&quiet";
            jobStatus.content = Guid.NewGuid().ToString();
            jobStatus.indexOfList = jobStatusList.Count;

            jobStatusList.Add(jobStatus);
        }

        private void timerElesped(Object status)
        {
            JobStatus jobStatus = (JobStatus)status;
            jobStatus.counter++;

            string errorMessage = string.Empty;
            string sendResult = string.Empty;
            sendResult = testSend(jobStatus.indexOfList, jobStatus.counter,
                jobStatus.userName, jobStatus.password, jobStatus.content, out errorMessage);
            textBox1.Text += "\r\n" + sendResult;

            jobStatusList[jobStatus.indexOfList] = jobStatus;


            if (jobStatus.counter == 5)
            {
                textBox1.Text += "\r\n" + "disposing of timer " + jobStatus.indexOfList;

                jobStatus.timer.Dispose();
                jobStatus.timer = null;

                //jobStatusList[jobStatus.indexOfList] = jobStatus;
                //return;
            }
        }

        private void btnJobOne_Click(object sender, EventArgs e)
        {
            JobStatus jobStatus = jobStatusList[0];
            jobStatus.timer.Change(-1, -1);
        }

        private void btnJobTwo_Click(object sender, EventArgs e)
        {
            JobStatus jobStatus = jobStatusList[1];
            jobStatus.timer.Change(-1, -1);
        }
    }

    class JobStatus
    {
        public int counter = 0;
        public System.Threading.Timer timer;
        public string userName;
        public string password;
        public string content;
        public int indexOfList;
    }
}